package zxing;

import java.io.FileNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.google.zxing.LuminanceSource;
import zxing.utils.CreateQRCode_Utils;
public final class RGBLuminanceSource extends LuminanceSource {
	private final byte[] luminances;

	public RGBLuminanceSource(String path) throws FileNotFoundException {
		this(loadBitmap(path));
	}

	public RGBLuminanceSource(Bitmap bitmap) {
		super(bitmap.getWidth(), bitmap.getHeight());
		//得到图片的宽�??  
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		//得到图片的像�??
		int[] pixels = new int[width * height];
		//
		bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
		// In order to measure pure decoding speed, we convert the entire image
		//为了测量纯解码�?�度，我们将整个图像灰度阵列前面，这是一样的通道
		// to a greyscale array
		// up front, which is the same as the Y channel of the
		// YUVLuminanceSource in the real app.
		
		//得到像素大小的字节数
		luminances = new byte[width * height];
		//得到图片每点像素颜色
		for (int y = 0; y < height; y++) {
			int offset = y * width;
			for (int x = 0; x < width; x++) {
				int pixel = pixels[offset + x];
				int r = (pixel >> 16) & 0xff;
				int g = (pixel >> 8) & 0xff;
				int b = pixel & 0xff;
				//当某�??点三种颜色�?�相同时，相应字节对应空间赋�?? 为其赋�??
				if (r == g && g == b) {
					// Image is already greyscale, so pick any channel.
					luminances[offset + x] = (byte) r;
				} else {
					 //其它情况字节空间对应赋�?�为�??
					// Calculate luminance cheaply, favoring green.
					luminances[offset + x] = (byte) ((r + g + g + b) >> 2);
//					luminances[offset + x] = (byte) ((0.299*r + 0.578*g + 0.114*b));
				}
			}
		}
	}

	@Override
	public byte[] getRow(int y, byte[] row) {
		if (y < 0 || y >= getHeight()) {
			throw new IllegalArgumentException(
					"Requested row is outside the image: " + y);
		}
		int width = getWidth();
		if (row == null || row.length < width) {
			row = new byte[width];
		}
		System.arraycopy(luminances, y * width, row, 0, width);
		return row;
	}

	// Since this class does not support cropping, the underlying byte array
	// already contains
	// exactly what the caller is asking for, so give it to them without a copy.
	@Override
	public byte[] getMatrix() {
		return luminances;
	}

	private static Bitmap loadBitmap(String path) throws FileNotFoundException {
		Bitmap bitmap = BitmapFactory.decodeFile(path);
		if (bitmap == null) {
			throw new FileNotFoundException("Couldn't open " + path);
		}
		return bitmap;
	}
	
	 /**扫描选择相册图片,判断是否压缩的临界�??*/
    public static final Long SCAN_DEFAULT_SIZE=(long)4000000; 
    /**
     * 4000000,我发现的问题手机3000000就挂�??,
     * �??以给了这个�?�具体的临界值我也不是很清楚,
     * 你可能会问为�??么要给个临界值盘判断是否压缩,那是因为有些小图压缩了不能解析识�??,
     * �??以我进行了判�??,大图压缩解析,小图直接解析
     * */
	private static Bitmap loadBitmap(String path, boolean isOkSize) throws FileNotFoundException {
        Bitmap bitmap = null;
        if (isOkSize) {  //小图,直接调用功能系统的解析返�??
            bitmap = BitmapFactory.decodeFile(path);
        } else { //大图 先压缩在返回
            bitmap = CreateQRCode_Utils.compress(path);
        }
        //ZXing源码
        if (bitmap == null) {
            throw new FileNotFoundException("Couldn't open " + path);
        }
        return bitmap;
    }
	
	
}

